<?php
require_once("includes/connect.php");
require_once("includes/function.php");

$tablename = $_POST["tablename"];
$dd = $_POST["dd"];
$fieldname = $_POST["fieldname"];
$idname = $_POST["idname"];
$by = $_POST["by"];

$ex1_id = $_POST["ex1_id"];
$ex2_id = $_POST["ex2_id"];
$ex3_id = $_POST["ex3_id"];

$db = new myDBS;

if ($by) {
$where = "expenses1_id in ($by)";
$result = $db->showData1($tablename, $where);
    
} else {
    $result = $db->showData($tablename);
}

//echo $where;
//die
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#ex1_id").change(function() {
            var by = $(this).val();

            //alert($(this).val());
            if (by != "เลือก...") {
                $.post('dropdown1_1.php', {tablename: 'expenses_lvl2', dd: 'ex2_id', idname: 'expenses2_id', fieldname: 'expenses2_name', by: by, ex2_id: '<?php echo $ex2_id; ?>', ex3_id: '<?php echo $ex3_id; ?>'}, function(data) {
                    $("#expenses2").html(data);
                });
            }

        });

        $("#ex1_id").trigger("change");
    });
</script>

<select class="span5" id="<?php echo $dd; ?>" name="<?php echo $dd; ?>" required>
    <option value="">เลือก...</option>
    <?php
    while ($rows = $db->fetch_object($result)) {
        if ($rows->{$idname} == $ex1_id) {
            $selected = " selected";
        } else {
            $selected = "";
        }
        ?>
        <option value="<?php echo $rows->{$idname}; ?>" <?php echo $selected; ?>><?php echo $rows->{$fieldname}; ?></option>
    <?php } ?>
</select>